package com.syedu.day12.homework;

import cn.hutool.core.util.ArrayUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * description：
 * 统计一段英文文章中每个单词出现的次数,然后按照降序排列结果
 *
 * 思路:
 * 1.indexOf()和substring(),递归操作(注意排重)
 * 2.数组和字符串来判断个数
 * 3.利用Map集合来处理
 *
 * @author Administrator
 * @date 2022/3/25 10:05
 */
public class Exercise1 {

    public static final Pattern PATTERN = Pattern.compile("[A-Za-z]+");

    public static void main(String[] args) {
        //一.字符串切割,注意切割符需要满足各种不同的标点符号要求
        /* String content = "There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do.May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy? Always put yourself in others’shoes.If you feel that it hurts you,it probably hurts the other person, too.";
        String[] words = content.split("[,.; ]+");*/

        //二.正则表达式
        String content = "There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do.May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy? Always put yourself in others’shoes.If you feel that it hurts you,it probably hurts the other person, too.";
        String[] words = {};
        Matcher matcher = PATTERN.matcher(content);
        while (matcher.find()) {
            String word = matcher.group();
            words = ArrayUtils.add(words, word);
        }
        System.out.println(Arrays.toString(words));
        //排重数组
        String[] distinctWords = ArrayUtil.distinct(words);
        System.out.println(Arrays.toString(distinctWords));
        //遍历排重数组,然后得到每一个单词,再原数组中去查看该单词出现多少次
        //定义实体类,封装单词和它出现的次数
        Pojo pojo = null;
        //定义数组保存封装的自定义对象,供最后的排序使用
        Pojo[] pojos = {};
        for (int i = 0; i < distinctWords.length; i++) {
            pojo = new Pojo(distinctWords[i], StringUtils.countMatches(content.toLowerCase(), distinctWords[i].toLowerCase()));
            pojos = ArrayUtils.add(pojos, pojo);
        }
        //排序规则,出现次数从大到小排序,如果次数相同,则按照单词的字典顺序排序
        Arrays.sort(pojos, (o1, o2) -> {
            int result = o2.getCount().compareTo(o1.getCount());
            if(result == 0){
                result = o1.getWord().compareTo(o2.getWord());
            }
            return result;
        });

        for(Pojo pj:pojos){
            System.out.println(pj);
        }
    }
}

@Data
@AllArgsConstructor
class Pojo {
    private String word;
    private Integer count;

    @Override
    public String toString(){
        return word + "," + count;
    }
}